package com.xuecheng.teaching.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.WorkDto;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.WorkVo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.api.ContentClient;
import com.xuecheng.teaching.domain.Work;
import com.xuecheng.teaching.domain.WorkRecord;
import com.xuecheng.teaching.mapper.WorkMapper;
import com.xuecheng.teaching.service.WorkRecordService;
import com.xuecheng.teaching.service.WorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 作业 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-12-18
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {

    @Autowired
    private ContentClient contentClient;//课程feign

    @Autowired
    private WorkRecordService workRecordService;//提交作业记录

    //分页查询
    @Override
    public ResponseResult<WorkVo> findByPage(PageRequest pageRequest, WorkDto workDto) {
        //构建分页
        Page<Work> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());
        //开启分页
        if (workDto!=null){
            page = this.lambdaQuery()
                    .like(Work::getTitle,workDto.getTitle())
                    .page(page);
        }else {
            page = this.lambdaQuery()
                    .page(page);
        }
        //查询结果转vo
        List<Work> workList = page.getRecords();
        List<WorkVo> workVoList =new ArrayList<>();
        for (Work work : workList) {
            WorkVo workVo = BeanHelper.copyProperties(work, WorkVo.class);
            //单独设置workId
            workVo.setWorkId(work.getId());
            if (work.getCreateDate()!=null){
                workVo.setCreateDate(work.getCreateDate().toString());//创建时间
            }
            if (work.getChangeDate()!=null){
                workVo.setChangeDate(work.getChangeDate().toString());//修改时间
            }
            ResponseResult<String> responseResult = contentClient.findTitleByWorkId(work.getId());
            if (responseResult.getCode()!=0){
                throw  new BusinessException(ErrorCode.DATAERROR);
            }
            //获取课程标题
            String courseName = responseResult.getData();
            workVo.setCourseName(courseName);
            //获取答题人数
            Long answerNum = workRecordService.lambdaQuery().eq(WorkRecord::getWorkId, work.getId()).count();
            workVo.setUserNum(answerNum.intValue());
            workVoList.add(workVo);
        }
        //返回结果
        return PageResponseResult.okResult(page.getTotal(),workVoList);
    }

    //新增或修改试卷
    @Override
    public ResponseResult saveOrUpdateWork(WorkDto workDto) {
        //校验前端参数
        if (!StrUtil.isAllNotEmpty(workDto.getQuestion(),workDto.getTitle())){
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //dto转数据库实体
        Work work = BeanHelper.copyProperties(workDto, Work.class);
        work.setId(workDto.getWorkId());//设置id
        work.setUsername(AuthInfoHolder.getUserId().toString());//创建人
        work.setStatus(1);//使用态
        work.setCreateDate(new Date());//创建时间
        if (work.getId()!=0){
            work.setChangeDate(new Date());
        }
        //保存数据库
        this.saveOrUpdate(work);
        //返回
        return ResponseResult.okResult();
    }

    //根据id查询试卷
    @Override
    public ResponseResult<WorkVo> findWorkById(Long workId) {
        //查询数据库
        Work work = this.getById(workId);
        //数据库实体转vo
        WorkVo workVo = BeanHelper.copyProperties(work, WorkVo.class);
        //返回结果
        return ResponseResult.okResult(workVo);
    }

    //根据id删除试卷
    @Override
    public ResponseResult deleteWork(Long workId) {
        this.removeById(workId);
        return ResponseResult.okResult();
    }
}
